/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package team;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Vector;
import javax.ejb.*;

/**
 *
 * @author denaas
 */
public class FootballTeamBean extends TeamBean implements EntityBean {

    private FootballTeam getFootballTeam() {
        return (FootballTeam) getTeam();
    }

    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">  
    /**
     * @see javax.ejb.EntityBean#ejbRemove()
     */
    public void ejbRemove() {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = getDataSource().getConnection();
            stmt = conn.prepareStatement("DELETE FROM footballteams WHERE id = ?");
            stmt.setInt(1, getFootballTeam().getId().intValue());

            if (stmt.executeUpdate() < 1) {
                throw new NoSuchEntityException();
            }

            super.ejbRemove();
        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }

    /**
     * @see javax.ejb.EntityBean#ejbLoad()
     */
    public void ejbLoad() {
        Object key = getContext().getPrimaryKey();
        if (key instanceof String) {
            getTeam().setId(new Integer((String) key));
        } else if (key instanceof Integer) {
            getTeam().setId((Integer) key);
        }

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = getDataSource().getConnection();
            stmt = conn.prepareStatement("Select * from footballteams where id = ?");
            stmt.setInt(1, getFootballTeam().getId().intValue());
            res = stmt.executeQuery();
            if (!res.next()) {
                throw new NoSuchEntityException();
            }

            getFootballTeam().setDraws(new Integer(res.getInt("draws")));
            getFootballTeam().setPoints(new Integer(res.getInt("points")));
            getFootballTeam().setGoalsScored(new Integer(res.getInt("goalsscored")));
            getFootballTeam().setGoalsAgainst(new Integer(res.getInt("goalsagainst")));
            getFootballTeam().setWins(new Integer(res.getInt("wins")));
            getFootballTeam().setLosses(new Integer(res.getInt("losses")));


            super.ejbLoad();
        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                res.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }

    /**
     * @see javax.ejb.EntityBean#ejbStore()
     */
    public void ejbStore() {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = getDataSource().getConnection();
            stmt = conn.prepareStatement("update footballteams set draws=?, points=?, goalsscored=?, goalsagainst=?, wins=?, losses=? where id = ?");
            stmt.setInt(1, getFootballTeam().getDraws().intValue());
            stmt.setInt(2, getFootballTeam().getPoints().intValue());
            stmt.setInt(3, getFootballTeam().getGoalsScored().intValue());
            stmt.setInt(4, getFootballTeam().getGoalsAgainst().intValue());
            stmt.setInt(5, getFootballTeam().getWins().intValue());
            stmt.setInt(6, getFootballTeam().getLosses().intValue());
            stmt.setInt(7, getFootballTeam().getId().intValue());

            if (stmt.executeUpdate() < 1) {
                throw new NoSuchEntityException();
            }


            super.ejbStore();
        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }

    // </editor-fold>
    /**
     * See EJB 2.0 and EJB 2.1 section 12.2.5
     */
    public FootballTeamBean() {
        setTeam(new FootballTeam());
    }

    public Integer ejbCreate(Team tm) throws CreateException {
        super.ejbCreate(tm);
        setTeam(tm);

        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = getDataSource().getConnection();
            stmt = conn.prepareStatement("INSERT INTO footballteams (draws, points, goalsscored, goalsagainst, wins, losses, id)"
                    + "VALUES (?,?,?,?,?,?,?)");
            stmt.setInt(1, getFootballTeam().getDraws().intValue());
            stmt.setInt(2, getFootballTeam().getPoints().intValue());
            stmt.setInt(3, getFootballTeam().getGoalsScored().intValue());
            stmt.setInt(4, getFootballTeam().getGoalsAgainst().intValue());
            stmt.setInt(5, getFootballTeam().getWins().intValue());
            stmt.setInt(6, getFootballTeam().getLosses().intValue());
            stmt.setInt(7, getFootballTeam().getId().intValue());

            if (stmt.executeUpdate() != 1) {
                throw new CreateException();
            }


            return getFootballTeam().getId();

        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }

    public java.lang.Integer ejbFindByPrimaryKey(java.lang.Integer aKey) throws ObjectNotFoundException {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = getDataSource().getConnection();
            stmt = conn.prepareStatement("select id from footballteams where id=?");
            stmt.setInt(1, aKey.intValue());
            res = stmt.executeQuery();

            if (!res.next()) {
                throw new ObjectNotFoundException();
            }


            super.ejbFindByPrimaryKey(aKey);
            return aKey;

        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                res.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }

    public Collection ejbFindAll() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = getDataSource().getConnection();
            stmt = conn.prepareStatement("select id from footballteams");
            res = stmt.executeQuery();

            Vector keys = new Vector();

            while (res.next()) {
                String empno = res.getString(1);
                keys.addElement(empno);
            }


            return keys;

        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                res.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }
}
